﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Web.Mvc;
using System.Net.Http;
using System.Net.Http.Headers;
using InternetClient2.Models;
using Newtonsoft.Json;
using System.Data;

namespace InternetClient2.Controllers
{
    public class InternetController : Controller
    {
        // GET: Internet
        public ActionResult Index()
        {
            return View();
        }

        [HttpGet]
        public ActionResult manage()
        {
            return View();
        }
        [HttpPost]
        public int manage(Jurisdiction news)
        {
           
            Uri uri = new Uri("http://localhost:52745/");
            HttpClient client = new HttpClient();
            client.BaseAddress = uri;
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("Application/json"));
         
            var json = JsonConvert.SerializeObject(news);
            HttpContent content = new StringContent(json);
            content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
            HttpResponseMessage message = client.PostAsync("/Internet/GetJurisdictionEnter",content).Result;
            
            int i = 0;
            if (message.IsSuccessStatusCode)
            {
                i = Convert.ToInt32(message.Content.ReadAsStringAsync().Result);

                return i;
            }
            else
            {
                 Content("<script>alert('调取失败')</script>");
            }
            return 0;
        }
        [HttpGet]
        public ActionResult denglu()
        {
            return View();
        }
        [HttpPost]
        public ActionResult denglu(Users news)
        {
            
            if (news.Uname == null)
            {
                ViewBag.Uname = "用户名不能为空";
                return View();
            }
            else if (news.UID_number == null)
            {

                ViewBag.UID_number = "身份证号不能空";
                return View();
            }
            Uri uri = new Uri("http://localhost:52745/");
            HttpClient client = new HttpClient();
            client.BaseAddress = uri;
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("Application/json"));

            var json = JsonConvert.SerializeObject(news);
            HttpContent content = new StringContent(json);
            content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
            HttpResponseMessage message = client.PostAsync("/Users/GetUsers", content).Result;

            int i = 0;
            if (message.IsSuccessStatusCode)
            {
                var q = message.Content.ReadAsStringAsync().Result;
                 string linq = JsonConvert.SerializeObject(q);
                
               
                //访问root.response;
                List<Users> list = JsonConvert.DeserializeObject<List<Users>>(q);

                i = (from item in list select item).Count();
                if (i >0)
                {
                    return Content("<script>alert('登录成功');location.href='/Users/Usersmain'</script>");
                }
                else 
                {
                    return Content("<script>alert('登录失败')</script>");
                }

            }
            else
            {
                return Content("<script>alert('调取失败')</script>");
            }
           
           
        }
       
    }
}